<template>
  <view class="box_content">
    <view class="item" v-for="(item, index) in listv" :key="index">
      <view class="item_left">
        <view v-show="!flag" class="item_left_clickView" @click="change(item)">
          <image
            v-if="!item.clickable"
            class="item_left_click"
            src="../../static/images/my/icon_grzy_quanxuan.png"
            mode=""
          ></image>
          <image
            v-else
            class="item_left_clickable"
            src="../../static/images/my/icon_grzy_xc_xuanzhong.png"
            mode=""
          ></image>
        </view>

        <image
          class="item_left_head"
          :src="item.user.avatar_url"
          mode=""
          @click="goHome(item.user.user_id)"
        ></image>
        <view class="item_left_name">{{ item.user.nick_name }}</view>
      </view>
      <view class="item_right" v-show="flag" @click="guanFn(item)">
        <view class="item_right_guanzhu" v-if="item.is_guanzhu == 0">
          <u-icon name="plus" color="#EEEEEE" size="15"></u-icon>
          <view class="item_right_guanzhu_text">关注</view>
        </view>
        <view class="item_right_yiguan" v-else>
          <view class="item_right_guanzhu_text">已关注</view>
        </view>
      </view>
    </view>

    <view class="bottom" v-show="!flag">
      <view class="bottom_left">
        <view @click="changeClick">
          <image
            v-if="!clickableAll"
            class="item_left_click"
            src="../../static/images/my/icon_grzy_quanxuan.png"
            mode=""
          ></image>
          <image
            v-else
            class="item_left_click"
            src="../../static/images/my/icon_grzy_xc_xuanzhong.png"
            mode=""
          ></image>
        </view>
        <view class="bottom_left_text">全选</view>
      </view>
      <view class="bottom_right" @click="deleteFn">删除</view>
    </view>

    <u-modal
      :show="show"
      :title="title"
      :content="content"
      confirmColor="#55B877"
      :closeOnClickOverlay="true"
      @confirm="confirm"
      @close="close"
      @cancel="cancel"
      :showCancelButton="true"
    ></u-modal>

    <view class="empty_view" v-if="listv && listv.length == 0">
      <image
        class="item_left_clickable"
        src="../../static/images/my/kong.png"
        mode=""
      ></image>
      <view>您还没有历史记录~</view>
    </view>
  </view>
</template>
  
  <script>
export default {
  components: {},
  props: {
    flag: {
      type: Boolean,
      default: true,
    },
    list:{
      type: Object,
    },
    from:{
      type: String,
      default: '',
    }
  },
  data() {
    return {
      clickableAll: false,
      listv:[],
      // list: [
      //   {
      //     clickable: false, // 当前是否选中
      //     name: "Monody",
      //     img: "../../static/images/pic_yt_xingzhegushi_one.png",
      //     guan: true, // 当前是否是关注
      //   },
      //   {
      //     clickable: false,
      //     name: "Monody",
      //     img: "../../static/images/pic_yt_xingzhegushi_one.png",
      //     guan: true,
      //   },
      //   {
      //     clickable: false,
      //     name: "Monody",
      //     img: "../../static/images/pic_yt_xingzhegushi_one.png",
      //     guan: false,
      //   },
      //   {
      //     clickable: false,
      //     name: "Monody",
      //     img: "../../static/images/pic_yt_xingzhegushi_one.png",
      //     guan: false,
      //   },
      //   {
      //     clickable: false,
      //     name: "Monody",
      //     img: "../../static/images/pic_yt_xingzhegushi_one.png",
      //     guan: true,
      //   },
      // ],
      show: false,
      title: "确认删除",
      content: "确认删除选中内容？",
    };
  },
  watch:{
    list:{
        deep: true, // 深度监听每一个属性值, 如果是对象可以深度监听
        immediate: true, // 进入页面后立即自动调用handler函数
        handler(newValue) {
            this.listv = newValue.data
            if(this.listv){
              for(let i=0;i<this.listv.length;i++){
                this.listv[i].clickable = false
              }
            }
            
            console.log(this.listv,'newList')
        }
      }
  },
  created() {
    
  },
  methods: {
    // 去个人主页
    goHome(user_id){
      uni.navigateTo({
        url: `/pages/home/home?user_id=${user_id}`,
      });
    },
    change(item) {
      this.$forceUpdate();
      item.clickable = !item.clickable;
      console.log(item.clickable);
      // 判断是否全选
      let all = true;
      for (let i = 0; i < this.listv.length; i++) {
        if (this.listv[i].clickable == false) {
          all = false;
        }
      }
      if (all) {
        this.clickableAll = true;
      } else {
        this.clickableAll = false;
      }
    },
    changeClick() {
      this.clickableAll = !this.clickableAll;
      if (this.clickableAll) {
        for (let i = 0; i < this.listv.length; i++) {
          this.listv[i].clickable = true;
        }
      } else {
        for (let i = 0; i < this.listv.length; i++) {
          this.listv[i].clickable = false;
        }
      }
    },
    guanFn(item) {
      console.log(item,'和黑欸')
      this.getGuan(item.user.user_id)
    },
    confirm() {
      this.show = false;
      let obj = {}
      let num = 0
      for(let i=0;i<this.listv.length;i++){
        if(this.listv[i].clickable == true){
          obj[num] = this.listv[i].id
          num++
        } 
      }
      this.deletFn(obj)
    },
    close() {
      this.show = false;
    },
    cancel() {
      this.show = false;
    },
    // 删除按钮
    deleteFn() {
      let isTrue = true;
      for (let i = 0; i < this.listv.length; i++) {
        console.log(this.listv[i].clickable);
        if (this.listv[i].clickable == true) {
          isTrue = false;
        }
      }
      if (isTrue == true) {
        uni.$u.toast("您还未选择内容");
      } else {
        this.show = true;
      }
    },
    // 删除接口
    deletFn(obj){
      this.$http
        .historyDel({ids:obj})
        .then((res) => {
          uni.$u.toast(res.message);
          this.$emit('updateData') 
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
    getGuan(user_id){
      this.$http
        .userGuanzhu({user_id:user_id})
        .then((res) => {
          uni.$u.toast(res.message);
          // setTimeout(() => {
            
          // }, 1000);
          this.$emit('updateData') 
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    }

  },
};
</script>
  
  <style scoped lang="scss">
.box_content {
  width: 100%;
  padding: 30rpx;
  box-sizing: border-box;
  .item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 32rpx;
    .item_left {
      display: flex;
      align-items: center;
      .item_left_clickView {
        margin-right: 24rpx;
        .item_left_click {
          width: 40rpx;
          height: 40rpx;
        }
        .item_left_clickable {
          width: 40rpx;
          height: 40rpx;
        }
      }

      .item_left_head {
        width: 100rpx;
        height: 100rpx;
        border-radius: 50%;
      }
      .item_left_name {
        margin-left: 24rpx;
        font-size: 32rpx;
        color: #eeeeee;
      }
    }

    .item_right {
      .item_right_guanzhu {
        width: 180rpx;
        height: 64rpx;
        background: #55b877;
        border-radius: 32rpx;
        font-size: 28rpx;
        color: #eeeeee;
        display: flex;
        align-items: center;
        justify-content: center;
        .item_right_guanzhu_text {
          margin-left: 11rpx;
        }
      }
      .item_right_yiguan {
        width: 180rpx;
        height: 64rpx;
        background: rgba(173, 173, 173, 0.102);
        border-radius: 32rpx;
        border: 1rpx solid #adadad;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 28rpx;
        color: #adadad;
      }
    }
  }

  .bottom {
    width: 100%;
    height: 112rpx;
    background: #272727;
    position: fixed;
    bottom: 0;
    left: 0;
    padding: 16rpx 30rpx;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .bottom_left {
      display: flex;
      align-items: center;
      .item_left_click {
        width: 40rpx;
        height: 40rpx;
      }
      .bottom_left_text {
        margin-left: 16rpx;
        color: #eeeeee;
        font-size: 30rpx;
      }
    }

    .bottom_right {
      width: 250rpx;
      height: 80rpx;
      background: #ee6060;
      border-radius: 48rpx;
      text-align: center;
      line-height: 80rpx;
      font-size: 30rpx;
      color: #eeeeee;
    }
  }

  /deep/.u-modal__content__text {
    text-align: center;
  }
  .empty_view {
    margin-top: 300rpx;
    image {
      margin: 0 auto;
      width: 410rpx;
      height: 410rpx;
    }
    view {
      font-size: 30rpx;
      color: #d8d8d8;
      text-align: center;
    }
  }
}
</style>